import { describe } from "mocha";
import * as util from '../src/util'
import { UploadInputs } from '../src/upload-inputs';
import * as packageUpload from '../src/package-upload';
import * as metaData from '../src/meta-data'
import path, { join } from "path";
import * as fs from 'fs';
import Sinon from 'sinon';
import proxyquire from 'proxyquire'


describe('UploadArtifact', () => {

    before(() => {
        const testTemp = path.join(__dirname, 'test_temp')
        if (!fs.existsSync(testTemp)) {
            fs.mkdirSync(testTemp)
            fs.writeFileSync(`${testTemp}${util.SEP}a.tgz`, 'test')
            fs.writeFileSync(`${testTemp}${util.SEP}a.txt`, 'test')
        }
    })
    after(() => {
        const testTemp = path.join(__dirname, 'test_temp')
        if (fs.existsSync(testTemp)) {
            util.clearDir(testTemp)
            fs.rmdirSync(testTemp)
        }
    })


    it('Successfully upload artifact to Packages', async() => {
        let inputs = {} as UploadInputs
        inputs.credential = 'Basic xxxxxx'
        inputs.workDir = path.join(__dirname, 'test_temp')
        inputs.artifact = 'xxxx-xxx'
        inputs.artifactName = 'xxxxx-xxx.tgz'
        inputs.artifactVersion = '2023-03-22'
        inputs.artifactFullName = path.join(__dirname, 'test_temp', 'a.tgz')
        inputs.packageHost = 'packagehost'
        inputs.orgId = 'orgid'
        inputs.repoId = 'repoid'
        inputs.fileMD5 = 'aa'
        let response = {
            data: {object: {fileMd5: 'aa', fileSize: 0, url: 'xx'}},
            status: 200
        }
        const axiosStub = Sinon.stub().resolves(response)
        const controller = proxyquire('../src/package-upload', {
            axios: axiosStub
        })
        await controller.artifactUpload(inputs)
    })

    it('Should not to upload metadata', async() => {
        let inputs = {} as UploadInputs
        inputs.artifact = 'xxxx-xxx'
        inputs.artifactName = 'xxxxx-xxx.tgz'
        inputs.artifactVersion = '2023-03-22'
        inputs.artifactFullName = path.join(__dirname, 'test_temp', 'a.tgz')
        inputs.packageHost = 'packagehost'
        inputs.repoId = 'orgid'
        inputs.regionId = 'repoid'
        inputs.fileMD5 = ''
        let mocker = Sinon.stub(metaData, 'getMetaData').resolves()

        let response = {
            data: {},
            status: 200
        }
        const axiosStub = Sinon.stub().resolves(response)
        const controller = proxyquire('../src/package-upload', {
            axios: axiosStub
        })
        await controller.uploadMetaData(inputs)
        Sinon.assert.calledOnce(mocker)
        mocker.restore()
    })

    it('Successfully upload metadata', async() => {
        let inputs = {} as UploadInputs
        inputs.artifact = 'xxxx-xxx'
        inputs.artifactName = 'xxxxx-xxx.tgz'
        inputs.artifactVersion = '2023-03-22'
        inputs.artifactFullName = path.join(__dirname, 'test_temp', 'a.tgz')
        inputs.packageHost = 'packagehost'
        inputs.repoId = 'orgid'
        inputs.regionId = 'repoid'
        inputs.fileMD5 = ''
        inputs.metaUpload = 'YES'
        let mocker = Sinon.stub(metaData, 'getMetaData').resolves()
        let response = {
            data: {},
            status: 200
        }
        const axiosStub = Sinon.stub().resolves(response)
        const controller = proxyquire('../src/package-upload', {
            axios: axiosStub
        })
        await controller.uploadMetaData(inputs)
        Sinon.assert.calledOnce(mocker)
        mocker.restore()
    })

})